﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using log4net;
using System.Text;
using AMebooking.NetUtils;
using System.Globalization;
using AMebooking.NetUtils.Settings;
using AMebooking.Models;
using System.Collections.Generic;
using AMebooking.Database;
using Newtonsoft.Json;

namespace AMebooking.Service
{
    public partial class Calendar : System.Web.UI.Page
    {
        protected static readonly ILog logger = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        protected static readonly StringBuilder sb = new StringBuilder();

        protected void Page_Load(object sender, EventArgs e)
        {
            //var filterTmpl = "{licId: \"134\", resId: \"S01\", deptId: \"0\"}";
            var defaultDatetimeFormat = Constants.DEFAULT_DATE_FORMAT;
            var startDate = StringUtils.DefaultIfBlank(Request.QueryString["startDate"], DateTime.Now.ToString(defaultDatetimeFormat));
            var endDate = StringUtils.DefaultIfBlank(Request.QueryString["endDate"], DateTime.Now.ToString(defaultDatetimeFormat));
            var licno = StringUtils.DefaultIfBlank(Request.QueryString["licno"], "");
            var reservationNo = StringUtils.DefaultIfBlank(Request.QueryString["reservationNo"], "");
            var deptid = StringUtils.DefaultIfBlank(Request.QueryString["deptid"], "");
            var smanid = StringUtils.DefaultIfBlank(Request.QueryString["smanid"], "");
            var siteid = StringUtils.DefaultIfBlank(Request.QueryString["siteid"], "");

            var startDateTime = DateTime.ParseExact(startDate, defaultDatetimeFormat, CultureInfo.InvariantCulture);
            var endDateTime = DateTime.ParseExact(endDate, defaultDatetimeFormat, CultureInfo.InvariantCulture);


            endDateTime = endDateTime.AddDays(1); ;

            //List<AMebooking.Models.Calendar> ret = new List<AMebooking.Models.Calendar>();
            //ret = DBAccess.getCalendar2(startDateTime, endDateTime,licno,reservationNo);
            //List<Reservation> ret = new List<Reservation>();
            //var workoderNo = -1;
            //if (!string.IsNullOrEmpty(reservationNo))
            //    workoderNo = Int32.Parse(reservationNo);

            //var ret = DBAccess.getCalendar(deptid, smanid, startDateTime, endDateTime, workoderNo);

            var ret = string.IsNullOrEmpty(licno) ? DBAccess.getMechanic(smanid, deptid, siteid) : DBAccess.getMechanicV2(smanid, deptid, siteid, licno);
            var res = new List<Timetable>();
            var di = startDateTime.Date;
            while (endDateTime.CompareTo(di) > 0)
            {
                foreach (var sman in ret)
                {
                    // fix avaibilities 
                    Timetable tt = new Timetable();
                    tt.StartHour = "07:15:00";
                    tt.EndHour = "17:30:00";
                    tt.WorkingDate = di.ToString("yyyy-MM-dd");
                    tt.SmanId = sman.smanid;
                    tt.Description = sman.name;
                    tt.Type = "P";
                    res.Add(tt);
                    // and breaks time
                    tt = new Timetable();
                    tt.StartHour = "12:00:00";
                    tt.EndHour = "13:15:00";
                    tt.WorkingDate = di.ToString("yyyy-MM-dd");
                    tt.SmanId = sman.smanid;
                    tt.Description = sman.name;
                    tt.Type = "T";
                    res.Add(tt);
                }
                di = di.AddDays(1);
            }

            var parsedData = "";
            parsedData = JsonConvert.SerializeObject(res);
            //for (int i = 0; i < ret.Count; )
            //{
            //    var obj = ret[i].ToString();
            //    parsedData += obj;
            //    if (++i < ret.Count)
            //    {
            //        parsedData += ",";
            //    }
            //}
            //parsedData = string.Format("[{0}]", parsedData);

            //var json1 = Services.getDept();
            //var json2 = Services.getMachenics();
            //var json = Services.getReservation("W", null, null, null, 100001);
            //json = "[" + parsedData + "]";
            var json = parsedData; // JsonConvert.SerializeObject(ret);

            Page.Response.ContentType = "application/json";
            Page.Response.Write(json);
            Page.Response.End();
        }
    }
}
